package com.literature.threeGroup.filter;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;

/**
 * @author VULCAN
 * @create 2020-01-24 10:59
 * @desc 拦截器
 **/
@Component
public class AccessFilter extends ZuulFilter {
    @Autowired
    private RedisTemplate redisTemplate;

    private static final Logger log = LoggerFactory.getLogger(AccessFilter.class);



    // 是否要过滤，false不过滤，true则需要过滤
            public boolean shouldFilter() {
        // TODO Auto-generated method stub
        return true;
    }
    @Override
    // 具体实施过滤逻辑的地方
        public Object run() throws ZuulException {
        RequestContext requestContext = RequestContext.getCurrentContext();
        HttpServletRequest request = requestContext.getRequest();
        HttpServletResponse response = requestContext.getResponse();
        StringBuffer url = request.getRequestURL();
        String url1 = (url + "");
        String url2 = ("http://29n01o8660.oicp.vip/server/userlogin");
        if (url1.equals(url2)) {
            // 是否登录
            boolean loginSuccess = true;
            // 是否权限认证成功
            boolean authOk = true;
            requestContext.setSendZuulResponse(true);

            return null;
        }
        // 是否登录
        boolean loginSuccess = false;
        // 是否权限认证成功
        boolean authOk = false;
        String loginId = (String) redisTemplate.opsForValue().get("loginId");
//        String loginId = request.getParameter("loginId");
        System.out.println(loginId);
//        String authId = request.getParameter("authId");
        String authId = (String) redisTemplate.opsForValue().get("tokenId");
        System.out.println(authId);
        // 假设传入的参数loginId不为空则代表登录成功,authId不为空则代表该访问有权限
        loginSuccess = StringUtils.isNotEmpty(loginId);
        authOk = StringUtils.isNotEmpty(authId);
        authOk = StringUtils.isNotEmpty(authId);
        if (loginSuccess && authOk) {
            log.debug("验证成功");
            requestContext.setSendZuulResponse(true);
            return null;
        }

        requestContext.setSendZuulResponse(false);
        // 不成功则根据请求类型进行处理

        if (isAjaxRequest(request)) {
            try {
                if (!loginSuccess) {
                    log.debug("未登录,非ajax请求,返回401");
                    response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "UNAUTHORIZED REQUEST");
            } else {
                    log.debug("鉴权失败,非ajax请求,返回403");
                    response.sendError(403, "UNAUTHORIZED REQUEST");

                }
            } catch (IOException e) {
                response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
                log.error("run", e);
            }
        } else {
            try {
                if (!loginSuccess) {
                    log.debug("未登录,非ajax请求,调转到指定页面");
                    turnPage(response, "http://localhost:8080/lyear_2_LightYear-admin/lyear_pages_login.html");

                } else {
                    log.debug("鉴权失败,非ajax请求,,返回403");
                    response.sendError(403, "UNAUTHORIZED REQUEST");
                }
                } catch (IOException e) {
                log.error("redirect fail", e);
            }
        }

        return null;
    }

    @Override
    // 过滤器类型
            public String filterType() {
        // pre：路由之前 routing：路由之时 post： 路由之后 error：发送错误调用
        return FilterConstants.PRE_TYPE;
    }

    @Override
    // 过滤的顺序
            public int filterOrder() {
        return 2;
    }

    /**
      * 判断是否是Ajax请求
      * 
      * @param request
      * @return
      */
            private boolean isAjaxRequest(HttpServletRequest request) {
        return (request.getHeader("accept") != null && request.getHeader("accept").contains("application/json"))
                || (request.getHeader("X-Requested-With") != null
                        && request.getHeader("X-Requested-With").contains("XMLHttpRequest"));
    }


    /**
      * 跳转页面
      * 
      * @param response
      * @param url      需要跳转页面的全路径
      */
            private void turnPage(HttpServletResponse response, String url) {
        try {
            response.setContentType("text/html; charset=utf-8");

            java.io.PrintWriter out = response.getWriter();
            out.println("<html><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /> ");
            out.println("<script>");
            out.println("window.open ('" + url + "','_top')");
            out.println("</script>");

            out.println("</html>");

            out.flush();
            out.close();



        } catch (IOException e) {
            log.error("responseHTML", e);
        }
    }
}


